package com.xj.api.bussiness.wxLogin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xj.api.base.web.BaseController;
import com.xj.api.bussiness.wxLogin.common.GetWxUserInfo;
import com.xj.api.bussiness.wxLogin.common.WxSession_key;
import com.xj.api.bussiness.wxLogin.service.IWxLoginService;
import com.xj.common.base.common.util.RandomStringGenerator;
import com.xj.common.bussiness.appUser.entity.TbAppUser;
import com.xj.common.bussiness.appUser.mapper.TbAppUserMapper;
import com.xj.common.bussiness.user.entity.TbUser;
import com.xj.common.bussiness.user.mapper.TbUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WxLoginServiceImpl extends BaseController implements IWxLoginService {

    @Autowired
    private GetWxUserInfo getWxUserInfo;

    @Autowired
    private TbAppUserMapper appUserMapper;

    @Autowired
    private TbUserMapper userMapper;
    @Override
    public HashMap<String, Object> GET(String url) {
        HashMap<String, Object> MAP = new HashMap<String, Object>();
        //HashMap<String, Object> rd_map = new HashMap<String, Object>();
        if (!url.isEmpty()) {
            String result = "";
            BufferedReader in = null;
            InputStream is = null;
            InputStreamReader isr = null;
            try {
                URL realUrl = new URL(url);
                URLConnection conn = realUrl.openConnection();
                conn.connect();
                Map<String, List<String>> map = conn.getHeaderFields();
                is = conn.getInputStream();
                isr = new InputStreamReader(is);
                in = new BufferedReader(isr);
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
            } catch (Exception e) {
                // 异常记录
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                    if (is != null) {
                        is.close();
                    }
                    if (isr != null) {
                        isr.close();
                    }
                } catch (Exception e2) {
                    // 异常记录
                }
            }
            if (!result.isEmpty()) {
                JSONObject object = JSONObject.parseObject(result);
                if (object.get("openid") != null && object.get("session_key") != null) {
                    String openid = object.get("openid").toString();
                    String session_key = object.get("session_key").toString();
                    //生成返回的key长168
                    String rd_session=RandomStringGenerator.getRandomStringByLength(168);
                    //redisStore.set(sessionid, openid + session_key, 7200)
                    MAP.put("openid", openid);
                    //MAP.put(openid, session_key);
                    MAP.put("status", "OK");
                    //全局变量每个帐号的session_key
                    WxSession_key.getWxSession().put(openid,session_key);
                    return MAP;
                }
            }else {
                MAP.put("status","appidAndSessionKey_Is_Exception");
            }
        }
        MAP.put("status","url_Is_Null");
        return MAP;
    }

    @Override
    public JSONObject getUserInfo(String encryptedData, String openid, String iv) {
        String sessionKey = WxSession_key.getWxSession().get(openid);
        JSONObject jsonObject=getWxUserInfo.getUserInfo(encryptedData,sessionKey,iv);
        return jsonObject;
    }

    @Override
    public HashMap<String,Object> haverWxRegister(String openid, String unionId) {
        List<TbAppUser> appUsersList = appUserMapper.selectList(new EntityWrapper<TbAppUser>().eq("wx_openid", openid).eq("wx_unionid",unionId));
        HashMap<String,Object> map=null;
        if(appUsersList.size()>0){
            map.put("userRole","0");
            map.put("user",appUsersList);
            return map;
        }else if(appUsersList.size() == 0){
            List<TbUser> userList=userMapper.selectList(new EntityWrapper<TbUser>().eq("u_wx_openid", openid).eq("u_wx_unionid",unionId));
            if(userList.size()>0){
                map.put("userRole","1");
                map.put("user",userList);
                return map;
            }
        }
        return map;
    }
}
